import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';

class UtilFun {
  static int daysBetween(DateTime a, DateTime b, [bool ignoreTime = false]) {
    if (ignoreTime) {
      int v = a.millisecondsSinceEpoch ~/ 86400000 -
          b.millisecondsSinceEpoch ~/ 86400000;
      if (v < 0) return -v;
      return v;
    } else {
      int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
      if (v < 0) v = -v;
      return v ~/ 86400000;
    }
  }


  static Future<bool> checkNetwork(context) async {
    var connectivityResult = await (Connectivity().checkConnectivity());

    if (connectivityResult == ConnectivityResult.mobile) {
      return showDialog<bool>(
        context: context,
        barrierDismissible:false,
        builder: (context) {
          return ButtonBarTheme(
            data: ButtonBarThemeData(alignment: MainAxisAlignment.center),
            child: AlertDialog(
              content: Text("当前网络为移动网络确定播放吗?"),
              contentPadding: EdgeInsets.fromLTRB(20, 20, 20, 0),
              actions: <Widget>[
                FlatButton(
                  child: Text("取消"),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  }, // 关闭对话框
                ),
                FlatButton(
                  child: Text("确定"),
                  onPressed: () {
                    //关闭对话框并返回true
                    Navigator.of(context).pop(true);
                  },
                ),
              ],
              shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            ),
          );
        },
      );
    } else if (connectivityResult == ConnectivityResult.wifi) {
      print("wifi");
      return true;
    }
  }
}